package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Jboss web模块各项指标的采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:53:53          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class WebCollector extends Collector {
    private static Logger logger = Logger.getLogger(WebCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        //        String getManagerName = this.getParamMap().get("名称").trim();
        String getManagerName = this.getParamMap().get("Name").trim();
        try {
            //            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("端口")));
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            Set<ObjectName> objectNames = RmiUtil.queryObjectNames(rmiAdapter, "jboss.web:j2eeType=WebModule,*");
            for (ObjectName jbossWeb : objectNames) {
                String webName = (String) rmiAdapter.getAttribute(jbossWeb, "objectName");
                if (getManagerName.equals(webName)) {
                    long currentDate = System.currentTimeMillis();
                    String keystring = ip + webName;
                    Long processingTimeValue = (Long) rmiAdapter.getAttribute(jbossWeb, "processingTime");
                    String processingTimeStr = processingTimeValue + "&" + currentDate;
                    String tmp = (String) getPersistentData(keystring);
                    if (tmp == null) {
                        savePersistentData(keystring, processingTimeStr);
                        return null;
                    }
                    String data[] = tmp.split("&");
                    Long lastDate = Long.parseLong(data[1]);
                    Long lastprocessingTimeValue = Long.parseLong(data[0]);

                    Long valuedata = processingTimeValue - lastprocessingTimeValue;
                    Float timedata = Float.parseFloat((currentDate - lastDate) + "") / 1000 / 60;
                    String str = timedata + "";
                    if (str.indexOf(".") > 0) {
                        timedata = Float.parseFloat(str.substring(0, str.lastIndexOf(".") + 2));
                    }

                    logger.info(" webName=" + webName + "\n processingTimeValue=" + processingTimeValue + ",currentDate=" + currentDate + "\n lastprocessingTimeValue=" + lastprocessingTimeValue + ",lastDate=" + lastDate + "\n valuedata=" + valuedata + ",timedata=" + timedata);

                    savePersistentData(keystring, processingTimeStr);

                    if (timedata == 0 || valuedata < 0)
                        return null;

                    propValues[0] = ResPropertyValue.createPropValue(processingTimeValue + "", propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue((valuedata / timedata) + "", propMetas[1]);
                    propertyValueX.setValues(propValues);
                    values.add(propertyValueX);
                    return values;
                }
            }
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
        return null;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "名称" };
        return new String[] { "Port", "Name" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "调用次数", "每分钟调用次数" };
        //        return new String[] { "CallTimes", "CallTimesPerMinute" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };
    }
}
